<template>
  <div class="dc-container">
    <!-- 根据业务字段多少，决定弹窗合理宽度 -->
    <!-- 不要使用`v-model:visible`和`:visible.sync` -->
    <el-dialog v-model="visible" width="50%"
      :fullscreen="false" :title="title" :close-on-click-modal="false"
      @open="onDialogOpen" @close="onDialogClose">
      <el-form ref="editForm" :model="form" :rules="rules" label-width="100px" :disabled="mode == 'view'">
        <!-- 根据业务字段多少，确定el-col的span值 -->
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item prop="code" label="编号">
              <el-input v-model="form.code" :maxLength="64" placeholder="请输入编号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="name" label="名称">
              <el-input v-model="form.name" :maxLength="128" placeholder="请输入名称" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item prop="ruleDef" label="规则">
              <el-input v-model="form.ruleDef" :maxLength="512" placeholder="请输入规则" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item prop="remarks" label="备注信息">
              <el-input v-model="form.remarks" :maxLength="255" type="textarea" :rows="3" placeholder="请输入备注信息" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 不要自定义footer -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onSubmit" type="primary" v-show="mode != 'view'">保存</el-button>
          <el-button @click="onDialogClose" v-if="mode != 'view'">取消</el-button>
          <el-button @click="onDialogClose" v-if="mode == 'view'">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { getCodeRuleById, saveCodeRule } from '@/api/sys/codeRule'
import BaseUI from '@/views/components/baseUI'
import OperationIcon from '@/components/OperationIcon'

export default {
  extends: BaseUI,
  name: 'codeRule-form',
  components: {
    OperationIcon
  },
  data() {
    return {
      // 操作模式 view/edit/add
      mode: 'add',
      form: this.initForm(),
      // 对话框属性变量
      visible: false,
      title: '系统编号规则',
      // 主表表单验证规则，根据数据库字段生成，columnNullable为true时，验证规则为空
      rules: {
        code: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        ruleDef: [{ required: true, message: '规则不能为空', trigger: 'blur' }]
      }
    }
  },
  props: {
    // 权限
    permission: {
      type: Object
    }
  },
  methods: {
    // 必须实现的方法
    async onSubmit() {
      let validFlag = true

      // 验证主表表单
      await this.$refs.editForm.validate((valid) => {
        if (!valid) {
          validFlag = false
          return false
        }
      })

      if (validFlag) {
        this.doSave()
      }
    },
    // 必须实现的方法
    doSave() {
      this.setLoad()
      saveCodeRule(this.form)
        .then((responseData) => {
          if (responseData.code == 100) {
            this.visible = false
            this.showMessage({
              type: 'success',
              msg: '保存成功'
            })
            this.$emit('save-finished')
          } else {
            this.showMessage(responseData)
          }
          this.resetLoad()
        })
        .catch((error) => {
          this.outputError(error)
        })
    },
    // 必须实现的方法
    getFormById(id) {
      this.setLoad()
      return new Promise((resolve) => {
        getCodeRuleById(id)
          .then((responseData) => {
            let form = {}
            if (responseData.code == 100) {
              form = responseData.data
            } else {
              this.showMessage(responseData)
            }
            this.resetLoad()
            resolve(form)
          })
          .catch((error) => {
            this.outputError(error)
          })
      })
    },
    // 必须实现的方法
    onDialogClose() {
      this.visible = false
    },
    // 必须实现的方法
    onDialogOpen() {
      this.$nextTick(() => {
        this.$refs.editForm.clearValidate()
      })
    },
    // 必须实现的方法 - 初始化表单数据
    initForm() {
      return {
        // 根据数据库字段生成前端对象属性
        code: '', // 编号
        name: '', // 名称
        ruleDef: '', // 规则
        remarks: '' // 备注信息
      }
    },
    // 必须实现的方法 - 打开查看模式
    async openView(id) {
      this.mode = 'view'
      this.title = `查看系统编号规则`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.visible = true
    },
    // 必须实现的方法 - 打开编辑模式
    async openEdit(id) {
      this.mode = 'edit'
      this.title = `修改系统编号规则`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.visible = true
    },
    // 必须实现的方法 - 打开新增模式
    openAdd(parent) {
      this.mode = 'add'
      this.title = `添加系统编号规则`
      this.form = this.initForm()
      this.visible = true
    },
    // 必须实现的方法 - 打开复制模式
    async openCopy(id) {
      this.mode = 'add'
      this.title = `添加系统编号规则`
      this.form = {
        ...this.initForm(),
        ...(await this.getFormById(id))
      }
      this.form.id = null //把id设置为空，添加一个新的
      this.visible = true
    }
  }
}
</script> 